package com.lianjinsu.shop.repository.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lianjinsu.shop.core.mybatis.AdvancedDaoImpl;
import com.lianjinsu.shop.entity.Orders;
import com.lianjinsu.shop.entity.enums.OrderStatus;
import com.lianjinsu.shop.repository.dao.OrdersDao;
import com.lianjinsu.shop.repository.mapper.OrdersMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class OrdersDaoImpl extends AdvancedDaoImpl<OrdersMapper, Orders> implements OrdersDao {

    @Override
    public List<Orders> selectByUser(String openId) {
        LambdaQueryWrapper<Orders> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Orders::getOpenid, openId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Orders> selectListOvertime(String before24H) {
        LambdaQueryWrapper<Orders> wrapper = Wrappers.lambdaQuery();
        wrapper.lt(Orders::getCreateTime, before24H);
        wrapper.eq(Orders::getStatus, OrderStatus.UNPAID);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Orders> selectByStatus(OrderStatus orderStatus) {
        LambdaQueryWrapper<Orders> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Orders::getStatus, orderStatus);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public int updateStatus(Orders orders) {
        LambdaUpdateWrapper<Orders> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(Orders::getTradeSn, orders.getTradeSn());
        return baseMapper.update(orders, wrapper);
    }
}
